//
//  GKBNavigationBar.m
//  GKBHouse
//
//  Created by  on 12-6-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GKBNavigationBar.h"
#import "GKBMainController.h"
#import "Global1.h"
#import "UIView+UIViewCategory.h"

#define kBarBtnWidth 66
#define kBarBtnHeight 26

@implementation GKBNavigationBar
@synthesize stateButton;
@synthesize titleLabel;
@synthesize refreshButton;
@synthesize returnButton;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)init
{
    self = [super init];
    if (self) {
        CGFloat height = kTopBarHeight;
        [self setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, height)];
        
        UIImage *topBG = [UIImage imageNamed:@"topBG.png"];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.frame];
        [imageView setImage:topBG];
        [self insertSubview:imageView atIndex:0];
        [imageView release];
        [self initView];
    }
    return self;
}
- (void)dealloc
{
    self.stateButton = nil;
    self.titleLabel = nil;
    self.refreshButton = nil;
    self.returnButton = nil;
    
    [super dealloc];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
        
}
- (void)initView
{
    NSInteger butLen = 66;
    // Drawing code
    // [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"topBG.png"]]];
    CGFloat startY = 2.0f + (IOS7_OR_LATER?00.0f:0.0f);
    CGFloat width = self.width;
    
//    //左边状态按钮
//    if (!self.stateButton) {
//        UIButton *state = [UIButton buttonWithType:UIButtonTypeCustom];
//        [state setFrame:CGRectMake(2, startY, butLen+20, 26)];
//        if ([LoginController1 loginType] == LoginTypeFar) {
//            [state setTitle:NSLocalizedString(@"Remote", nil) forState:UIControlStateNormal]; 
//        }
//        else
//        {
//            [state setTitle:NSLocalizedString(@"Local", nil) forState:UIControlStateNormal]; 
//        }
//        [state.titleLabel setFont:kTitleFontBold]; 
//        self.stateButton = state;
//        [self addSubview:self.stateButton];        
//    }
    //中间标题
    if (!self.titleLabel) {
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(2 + butLen, startY, width - (2 + butLen) * 2, 26)];
        [title setText:NSLocalizedString(@"gkbHome", nil)];
        [title setTextColor:[UIColor whiteColor]];
        [title setTextAlignment:UITextAlignmentCenter];
        [title setBackgroundColor:[UIColor clearColor]];
        [title setFont:kTitleFontBold];
        self.titleLabel = title;
        [self addSubview:self.titleLabel];  
        [title release];
    }
    //right button
    if (!self.returnButton) {
        UIButton *refresh = [UIButton buttonWithType:UIButtonTypeCustom];
        [refresh setFrame:CGRectMake(width - (2 + butLen)*2 , startY, butLen, 26)];
        [refresh setTitle:NSLocalizedString(@"refresh", nil) forState:UIControlStateNormal]; 
        
        [refresh setBackgroundImage:[UIImage imageNamed:@"buttonBlue.png"] forState:UIControlStateNormal];
        [refresh setBackgroundImage:[UIImage imageNamed:@"buttonBlueSelected.png"] forState:UIControlStateHighlighted];
        [refresh addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventTouchUpInside];
        //[menu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //[menu setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        //[menu setBackgroundColor:[UIColor blueColor]];
        self.refreshButton = refresh;
        [self addSubview:self.refreshButton];
      //  self.refreshButton.hidden = YES;
    }

    if (!self.returnButton) {
        UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
        [returnBut setFrame:CGRectMake(width - 2 -butLen, startY, butLen, 26)];
        [returnBut setTitle:NSLocalizedString(@"menu", nil) forState:UIControlStateNormal]; 
        [returnBut.titleLabel setFont:kTitleFontBold];
       // [returnBut setImage:[UIImage imageNamed:@"backBut.png"] forState:UIControlStateNormal];
        [returnBut setBackgroundImage:[UIImage imageNamed:@"buttonBlue.png"] forState:UIControlStateNormal];
        [returnBut setBackgroundImage:[UIImage imageNamed:@"buttonBlueSelected.png"] forState:UIControlStateHighlighted];
        [returnBut addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
        //[menu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //[menu setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        //[menu setBackgroundColor:[UIColor blueColor]];
        self.returnButton = returnBut;
        [self addSubview:self.returnButton];
    }
}
- (void)setTarget:(id)target returnAction:(SEL)action withTitle:(NSString*)title
{
    self.refreshButton.hidden = YES;
    
//    [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"topBG.png"]]];
    [self.returnButton setImage:[UIImage imageNamed:@"backBut.png"] forState:UIControlStateNormal];
    [self.returnButton setTitle:@"" forState:UIControlStateNormal];
    [self.returnButton removeTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
    [self.returnButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self.titleLabel setText: title];
}
- (void)refreshAction
{
    GKBMainController *main = [GKBMainController mainCtr];
    if (main) {
        [main equipmentStateWidthTip];
    }
}
- (void)openMenu
{
    if (!popMenu) {
        popMenu = [[MenuController alloc]initWithNibName:@"MenuController" bundle:nil];
    }
    [popMenu.view setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH)];
    
   // [[UIApplication sharedApplication].keyWindow addSubview:menu.view];
    [[GKBMainController mainView] addSubview:popMenu.view];

 //   [menu release];
}
- (void)removeFromSuperview
{
    [super removeFromSuperview];
    [popMenu release];
}

+ (CGRect)rightBtnRect
{
    CGFloat startY = 2.0f + (IOS7_OR_LATER?20.0f:0.0f);
    CGFloat width = DEVICE_HEIGHT;
    
    return CGRectMake(width - 2 -kBarBtnWidth, startY, kBarBtnWidth, kBarBtnHeight);
}

+ (CGRect)rightBtn2Rect
{
    CGFloat startY = 2.0f + (IOS7_OR_LATER?20.0f:0.0f);
    CGFloat width = DEVICE_HEIGHT;
    
    return CGRectMake(width - (2 + kBarBtnWidth)*2 , startY, kBarBtnWidth, kBarBtnHeight);
}

+ (CGRect)rightBtnRect1
{
    CGFloat startY = 2.0f;
    CGFloat width = DEVICE_HEIGHT;
    
    return CGRectMake(width - 2 -kBarBtnWidth, startY, kBarBtnWidth, kBarBtnHeight);
}

+ (CGRect)rightBtn2Rect1
{
    CGFloat startY = 2.0f;
    CGFloat width = DEVICE_HEIGHT;
    
    return CGRectMake(width - (2 + kBarBtnWidth)*2 , startY, kBarBtnWidth, kBarBtnHeight);
}

@end

@implementation NormalApp

@end
